package com.ziyun.erp.web.modules.order.controller;

import com.ziyun.erp.common.utils.UserUtils;
import com.ziyun.erp.modules.order.dto.OrderItemAuditDto;
import com.ziyun.erp.modules.order.entity.OrderItemEntity;
import com.ziyun.erp.modules.order.vo.OrderItemQuoteVo;
import com.ziyun.erp.modules.order.vo.OrderItemVo;
import com.ziyun.erp.modules.order.service.OrderItemService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.common.validator.ValidatorUtils;
import io.renren.modules.sys.entity.SysUserEntity;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 订单详情表
 *
 * @author gaosheng
 * @email
 * @date 2018-08-07 15:31:52
 */
@RestController
@RequestMapping("order/orderitem")
public class OrderItemController {

    @Autowired
    private OrderItemService orderItemService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("order:orderitem:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = orderItemService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{itemId}")
    @RequiresPermissions("order:orderitem:info")
    public R info(@PathVariable("itemId") Integer itemId){
        OrderItemEntity orderItem = orderItemService.selectById(itemId);

        return R.ok().put("orderItem", orderItem);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("order:orderitem:save")
    public R save(@RequestBody OrderItemEntity orderItem){
        orderItemService.insert(orderItem);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("order:orderitem:update")
    public R update(@RequestBody OrderItemEntity orderItem){
        ValidatorUtils.validateEntity(orderItem);
        orderItemService.updateAllColumnById(orderItem);//全部更新
        
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("order:orderitem:delete")
    public R delete(@RequestBody Integer[] itemIds){
        orderItemService.deleteBatchIds(Arrays.asList(itemIds));

        return R.ok();
    }

    /**
     * 获取订单明细信息
     */
    @RequestMapping("/itemVos/{orderId}")
    public R itemVos(@PathVariable("orderId") String orderId){
        List<OrderItemVo> orderItemVos = orderItemService.selectOrderItemVosById(Long.valueOf(orderId));

        return R.ok().put("orderItems", orderItemVos);
    }

    /**
     * 订单商品审核
     * @param orderItemAuditDto
     * @return
     */
    @RequestMapping("/orderItemAudit")
    public R orderItemAudit(@RequestBody OrderItemAuditDto orderItemAuditDto){
        SysUserEntity curentUser = UserUtils.getCurentUser();
        Long userId = curentUser.getUserId();
        String username = curentUser.getUsername();
        return orderItemService.orderItemAudit(orderItemAuditDto, userId, username);
    }

    /**
     * 报价商品计算过程解析
     */
    @RequestMapping("/resolveQuoteItem/{itemId}")
    public R resolveQuoteItem(@PathVariable("itemId") Integer itemId){
        OrderItemQuoteVo orderItemQuoteVo = orderItemService.resolveQuoteItem(itemId);

        return R.ok().put("orderItemQuote", orderItemQuoteVo);
    }

}
